package com.liu.sunnyday.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.liu.sunnyday.constant.MessageConstant;
import com.liu.sunnyday.constant.RedisMessageConstant;
import com.liu.sunnyday.entity.Result;
import com.liu.sunnyday.exception.SunnydayException;
import com.liu.sunnyday.pojo.Order;
import com.liu.sunnyday.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.Map;

/**
 * chance！Time
 *
 * @author liuqian
 * @date 2022-06-30 15:14
 * *
 */
@RestController
@RequestMapping("/orderm")
public class OrderMobileController {
    @Reference
    private OrderService orderService;

    @Autowired
    private JedisPool jedisPool;

    /**
     * 提交预约
     *
     * @param paraMap
     * @return
     */
    @PostMapping("/submit")
    public Result submit(@RequestBody Map<String, String> paraMap) throws SunnydayException {
        Jedis jedis = jedisPool.getResource();
        //1，拿到表单里的数据
        String telephone = paraMap.get("telephone");
        String keyname = RedisMessageConstant.SENDTYPE_ORDER + "_" + telephone;
        //拿到redis中该key中value的数据
        String codeInRedis = jedis.get(keyname);
        //对比redis中的验证码和用户输入的验证码
        String validateCode = paraMap.get("validateCode");
        if (!codeInRedis.equals(validateCode)) {
            //如果不相同
            return new Result(true, MessageConstant.VALIDATECODE_ERROR);
        }
        Order order = orderService.submitOrder(paraMap);

        return new Result(true, MessageConstant.ORDER_SUCCESS, order);
    }

    /**
     * 预约成功回写
     *
     * @param id
     * @return
     */
    @GetMapping("/findById")
    public Result findById(Integer id) {
        Map<String, Object> orderinfo = orderService.findOrderDetailById(id);
        return new Result(true, MessageConstant.ORDER_SUCCESS,orderinfo);
    }
}
